package com.leyou.page.web;

import com.leyou.page.service.PageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.Map;

@Controller
public class PageController {

    @Autowired
    private PageService pageService;

    @GetMapping("/item/{id}.html")
    public String item(Model model, @PathVariable("id") Long id){
        System.out.println("id="+id);
        // 给item.thml页面提供值
        Map<String, Object> itemMap = pageService.loadItemData(id);
        // 把数据存入model
        model.addAllAttributes(itemMap);
        // 查询商品信息
        return "item";
    }

    /**
     *   回顾：SpringMVC中有个视图解析器： InternalResourceViewResolver
     *      prefix: 前缀：   /WEB-INF/jsp
     *      suffix: 后缀：   .jsp
     *
     *      寻找jsp路径  prefix + 视图名称  +  suffix
     *
     *
     *  SpringBoot中： Thymeleaf的默认配置
     * - 默认前缀：classpath:/templates/
     * - 默认后缀：.html
     *
     * 所以如果我们返回视图：hello，会指向到 classpath:/templates/hello.html
     *
     * @param model
     * @return   视图名称
     */
    @GetMapping("/hello")
    public String hello(Model model){
        model.addAttribute("msg", "小飞飞真的好纯啊！");
        return "hello";
    }
}
